package com.nuanshui.heatedloan.controller.manage.accountbill;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.lang.MoneyUtil;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.fund.enums.PaymentBizType;
import com.nuanshui.heatedloan.fund.facade.UserAccountBillService;
import com.nuanshui.heatedloan.fund.facade.UserBindAccountService;
import com.nuanshui.heatedloan.fund.facade.UserFundActionService;
import com.nuanshui.heatedloan.fund.integration.accountbill.AccountBillFacade;
import com.nuanshui.heatedloan.fund.model.UserAccountBill;
import com.nuanshui.heatedloan.msgcenter.constant.SmsMsgBizType;
import com.nuanshui.heatedloan.msgcenter.integration.MsgTools;
import com.nuanshui.heatedloan.msgcenter.model.AliSmsNotificationCodeTemp;
import com.nuanshui.heatedloan.msgcenter.model.SmsPushMsgBody;
import com.nuanshui.heatedloan.userinfo.facade.UserAuthService;
import com.nuanshui.heatedloan.userinfo.facade.UserQueryService;
import com.nuanshui.heatedloan.userinfo.facade.UserService;
import com.nuanshui.heatedloan.userinfo.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员资金管理
 *
 * @author Junyan2015
 * @date 2016年2月18日 上午10:04:02
 */
@RestController
@RequestMapping("/manage/accountBill/")
public class ManagerAccountBillController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Reference(url = GlobalParam.USER_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.USER_DUBBO_SERVER_VERSION)
    private UserService userService;

    @Reference(url = GlobalParam.FUND_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.FUND_DUBBO_SERVER_VERSION)
    private UserBindAccountService userBindAccountService;
    @Reference(url = GlobalParam.FUND_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.FUND_DUBBO_SERVER_VERSION)
    private UserFundActionService userFundActionService;
    @Reference(url = GlobalParam.FUND_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.FUND_DUBBO_SERVER_VERSION)
    private UserAccountBillService userAccountBillService;

    @Reference(url = GlobalParam.FUND_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.FUND_DUBBO_SERVER_VERSION)
    private UserAuthService userAuthService;

    @Autowired
    private AccountBillFacade accountBillFacade;
    @Autowired
    private MsgTools msgTools;

    @Reference(url = GlobalParam.USER_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.USER_DUBBO_SERVER_VERSION)
    private UserQueryService userQueryService;


    /**
     * 管理员获得所有用户账单信息
     *
     * @param num   第几个点击块
     * @param pager
     * @return
     * @author Junyan2015
     * @date 2016年2月18日 下午1:54:28
     */
    @RequestMapping(value = "listUserAccountBill",method = RequestMethod.POST)
    public String listUserAccountBill(Integer num, Pageable pager) {
        LogUtil.info(logger, "根据类型:" + num + "获得所有用户的账单信息");
        String userId = getCurrentUser().getId();
        Integer isAdmin = userAuthService.isAdministrator(userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("isAdmin", isAdmin);
        List<UserAccountBill> list = userAccountBillService.findAllUserAccountBill(num, pager);
        for (UserAccountBill accountBill : list) {
            User user = userQueryService.load(accountBill.getUserId());
            if (user != null) {
                accountBill.setUserNick(user.getNick());
                accountBill.setActionName(accountBill.getUserNick() + accountBill.getActionName());
            }
        }
        map.put("list", list);
        return success(map);
    }

    /**
     * 管理员获得某个用户账单详情
     *
     * @param billId 账单ID
     * @return
     * @author Junyan2015
     * @date 2016年2月18日 下午1:54:45
     */
    @RequestMapping(value = "billDetail",method = RequestMethod.POST)
    public String getBillDetail(String billId) {
        LogUtil.info(logger, "管理员获得某个用户:" + billId + "账单详情");
        LogUtil.info(logger, "根据账单id" + billId + "获得账单详情");
        Map<String, Object> map = accountBillFacade.getAccountDetail(billId);
        return success(map);
    }

    /**
     * 管理员审核不同类型账单
     *
     * @param billId
     * @param type
     * @param isPass 是否通过，1表示通过，0表示不通过
     * @return
     * @author Junyan2015
     * @date 2016年2月18日 下午1:55:12
     */
    @RequestMapping(value = "checkUserAction",method = RequestMethod.POST)
    public String checkUserAction(String billId, String type, Integer isPass) {
        LogUtil.info(logger, "管理员根据type:" + type + ",billId:" + billId + ",是否通过:" + isPass + "审核不同类型账单");
        boolean result = userAccountBillService.checkUserAction(billId, type, isPass);
        try {
            sendMsg(billId, isPass);
        } catch (Exception e) {
            LogUtil.info(logger, "发送短信通知失败", e);
        }
        return success(result);
    }


    /**
     * 给用户发送 短信
     *
     * @param
     * @author guohao
     * @date 2016年2月19日 上午11:39:01
     */
    private void sendMsg(String billId, Integer isPass) {
        LogUtil.info(logger, "根据isPass:" + isPass + "来判断是否发送短信");
        UserAccountBill userAccountBill = userAccountBillService.findBillDetail(billId);
        SmsPushMsgBody model = new SmsPushMsgBody();
        model.addDstUserId(userAccountBill.getUserId());
        model.setBizId(userAccountBill.getBizId());
        model.putParam("money", MoneyUtil.toDoubleString(userAccountBill.getMoney() * -1));
        if (PaymentBizType.WITHDRAW_CASH.getCode().equals(userAccountBill.getType())) {
            if (isPass == 1) {

                model.setBizType(SmsMsgBizType.APPLY_DRAW_CASH_OK);
                model.setTemplateCode(AliSmsNotificationCodeTemp.APPLY_DRAW_CASH_OK.gettempCode());
                //如果是提现审核成功发送短信
                msgTools.sendSmsMsg(model);
            } else if (isPass == 0) {//不通过发送短息

                model.setBizType(SmsMsgBizType.APPLY_DRAW_CASH_REFUSE);
                model.setTemplateCode(AliSmsNotificationCodeTemp.APPLY_DRAW_CASH_REFUSE.gettempCode());
                msgTools.sendSmsMsg(model);
            }
        }
    }

}
